package com.itheima.redbaby.activity;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Order;
import com.itheima.redbaby.engine.OrderEngine;
import com.itheima.redbaby.engine.impl.OrderEngineImpl;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.StreamToString;

public class OrderActivity extends BaseActivity implements OnClickListener {

	// 1=>近一个月订单 2=>一个月前订单 3=>已取消订单
	public static final int THIS_MONTH = 1;
	public static final int BEFORE_MONTH = 2;
	public static final int CANCEL_ORDER = 3;
	
	//显示订单状态
	private int state;
	
	
	private Intent intent;

	private TextView my_order_month;
	private TextView my_order_month_before;
	private TextView my_order_notsend;
	private ListView my_order_list;
	private TextView my_order_null_text;
	private OrderAdapter adapter;
	private List<Order> orderList;
	
	
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			
		}

	};
	private TextView head_back_text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.my_order_activity);

		initView();
	}

	private void initView() {
		 
		orderList=new ArrayList<Order>();
		
		
		my_order_month = (TextView) findViewById(R.id.my_order_month);
		my_order_month_before = (TextView) findViewById(R.id.my_order_month_before);
		my_order_notsend = (TextView) findViewById(R.id.my_order_notsend);
		my_order_list = (ListView) findViewById(R.id.my_order_list);
		my_order_null_text = (TextView) findViewById(R.id.my_order_null_text);
		
		head_back_text = (TextView) findViewById(R.id.head_back_text);
		
		my_order_month.setOnClickListener(this);
		my_order_month_before.setOnClickListener(this);
		my_order_notsend.setOnClickListener(this);
		head_back_text.setOnClickListener(this);
		
		state=THIS_MONTH;
		getData(state);
		
		adapter = new OrderAdapter();
		my_order_list.setAdapter(adapter);
		
		//项目点击
		my_order_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				intent=new Intent(OrderActivity.this,OrderDetailActivity.class);
				Order order=orderList.get(position);
				intent.putExtra("order", order);
				//传递点击的order位置，返回结果码时删除此order
				intent.putExtra("position", position);
				startActivityForResult(intent, 200);
			}
			
		});

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==100)
		{
			Bundle extras = data.getExtras();
			int orderPosition=extras.getInt("deletePositon");
			orderList.remove(orderPosition);
			updateAdapter();
		}
	}

	/**
	 * 获取数据
	 */
	private void getData(final int type) {
		new MyHttpTask() {
			@Override
			protected Object doInBackground(Void... params) {
				OrderEngine impl = DaoAndEngineFactory
						.getImpl(OrderEngine.class);
				orderList = impl.getOrderList(type);
				return null;
			}

			protected void onPostExecute(Object result) {
				updateAdapter();
			};
		}.executeHttp();

	}
	 

	protected void updateAdapter() {
		if(orderList.size()>0)
		{
			my_order_list.setVisibility(View.VISIBLE);
			my_order_null_text.setVisibility(View.GONE);
			
			
			adapter.notifyDataSetChanged();
		}else{
			my_order_list.setVisibility(View.GONE);
			my_order_null_text.setVisibility(View.VISIBLE);
		}
	
		
	}

	class OrderAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return orderList.size();
		}

		@Override
		public Object getItem(int position) {
			return orderList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(OrderActivity.this,
						R.layout.my_order_listitem, null);
				holder.orderId_text = (TextView) convertView
						.findViewById(R.id.orderId_text);
				holder.orderPrice_text = (TextView) convertView
						.findViewById(R.id.orderPrice_text);
				holder.orderTime_text = (TextView) convertView
						.findViewById(R.id.orderTime_text);
				holder.orderState_text = (TextView) convertView
						.findViewById(R.id.orderState_text);
				holder.textCancelOrder=(TextView) convertView.findViewById(R.id.textCancelOrder);
				convertView.setTag(holder);
			}
			View holderconvertView;
			holder = (ViewHolder) convertView.getTag();
			holder.orderId_text
					.setText(orderList.get(position).getOrder_id());
			holder.orderPrice_text
					.setText(orderList.get(position).getPrice() + "");
			String status = orderList.get(position).getStatus();
			if("3".equals(status))
				holder.orderState_text.setText("已完成");
			if("1".equals(status))
				holder.orderState_text.setText("未处理");
			if("2".equals(status))
				holder.orderState_text.setText("交易成功");
			if(orderList.get(position).getType()==3)
				holder.orderState_text.setText("已取消");
				
			holder.orderTime_text.setText(orderList.get(position).getTime());
			
			if(state==CANCEL_ORDER){
				holder.textCancelOrder.setVisibility(View.GONE);
			}else{
				holder.textCancelOrder.setVisibility(View.VISIBLE);
			}
			
			holder.textCancelOrder.setOnClickListener(new OnClickListener() {
				 
				@Override
				public void onClick(View v) {
					cancelOrder(orderList.get(position).getOrder_id());
				}
			});
			return convertView;
		}

		
	}
	protected void cancelOrder(final String order_id) {
		new MyHttpTask() {
			
			@Override
			protected Object doInBackground(Void... params) {
				OrderEngine impl = DaoAndEngineFactory.getImpl(OrderEngine.class);
				boolean b = impl.cancelOrder(order_id);
				
				return b;
			}
			
			protected void onPostExecute(Object result) {
				getData(state);
			};
			
		}.executeHttp();
	}

	class ViewHolder {
		TextView orderId_text;
		TextView orderPrice_text;
		TextView orderTime_text;
		TextView orderState_text;
		TextView orderPayment_text;
		TextView textCancelOrder;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.my_order_month:
			state=THIS_MONTH;
			getData(state);
			my_order_month.setBackgroundResource(R.drawable.segment_selected_1_bg);
			my_order_month_before.setBackgroundResource(R.drawable.segment_normal_2_bg);
			my_order_notsend.setBackgroundResource(R.drawable.segment_normal_3_bg);
			
			my_order_month.setTextAppearance(OrderActivity.this, R.style.style_13_FFFFFF_BOLD);
			my_order_month_before.setTextAppearance(OrderActivity.this, R.style.style_13_4B4B4B_BOLD);
			my_order_notsend.setTextAppearance(OrderActivity.this, R.style.style_13_4B4B4B_BOLD);
			
			
			break;
		case R.id.my_order_month_before:
			
			state=BEFORE_MONTH;
			getData(state);
			
			my_order_month.setBackgroundResource(R.drawable.segment_normal_1_bg);
			my_order_month_before.setBackgroundResource(R.drawable.segment_selected_2_bg);
			my_order_notsend.setBackgroundResource(R.drawable.segment_normal_3_bg);
			
			my_order_month.setTextAppearance(OrderActivity.this, R.style.style_13_4B4B4B_BOLD);
			my_order_month_before.setTextAppearance(OrderActivity.this, R.style.style_13_FFFFFF_BOLD);
			my_order_notsend.setTextAppearance(OrderActivity.this, R.style.style_13_4B4B4B_BOLD);
			
			break;
		case R.id.my_order_notsend:
			
			state=CANCEL_ORDER;
			getData(state);
			my_order_month.setBackgroundResource(R.drawable.segment_normal_1_bg);
			my_order_month_before.setBackgroundResource(R.drawable.segment_normal_2_bg);
			my_order_notsend.setBackgroundResource(R.drawable.segment_selected_3_bg);
			
			my_order_month.setTextAppearance(OrderActivity.this, R.style.style_13_4B4B4B_BOLD);
			my_order_month_before.setTextAppearance(OrderActivity.this, R.style.style_13_4B4B4B_BOLD);
			my_order_notsend.setTextAppearance(OrderActivity.this, R.style.style_13_FFFFFF_BOLD);
			
			break;
			
		case R.id.head_back_text:
			finish();
			break;

		}
	}

}
